package com.lanou.web;
import com.lanou.domain.CrmDepartment;
import com.lanou.domain.CrmStaff;
import com.lanou.service.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.List;

/**
 * action层
 */
public class StaffAction extends ActionSupport {

    //不直接使用实现类，使用接口
    private StaffService staffService;
    CrmStaff crmStaff;
    //-----------------------------------------------
    //用户登录验证
    @Override
    public String execute() throws Exception {
        //调用service层方法
        CrmStaff staff = staffService.find(crmStaff);
        if (staff != null) {
            //成功，在session作用域中保存数据
            ActionContext.getContext().getSession().put("loginStaff", staff);
            return SUCCESS;
        }
        //不成功
        //添加错误信息 在首页errorMessage
        addFieldError("errorMessage", "用户名密码错误");
        return LOGIN;
    }
//-----------------------------------------------
    //员工管理，显示所有的员工数
    public String findAll(){
        //查询所有
        List<CrmStaff> allStaff=staffService.findAll();
        //将结果放到值栈，使用context存
        ActionContext.getContext().put("allStaff",allStaff);
        return "allStaff";
    }


//-----------------------------------------------
//   接收前端url中拼接过来的数据，staffid，根据id查询（提供get/set方法，与前端属性名字相同，用于接收staffId）
    private  String staffId;

    DepService depService;
    //接收部门信息
    List<CrmDepartment> allDepartment;

    //根据id查询结果，回显在界面
    public String preEdit(){
        //从前端传过来的id信息，通过id查询一条记录，存储在值栈中
        System.out.println(staffId);
        //根据id查询一条数据，显示在页面,将值放入值栈中
        CrmStaff findStaff=staffService.findById(staffId);
        ActionContext.getContext().getValueStack().push(findStaff);

        //部门和职务回显
        System.out.println("部门和职务回显.....");
        allDepartment=depService.findAll();
        //将返回值放入值栈中
        System.out.println(allDepartment+"111111");
        ActionContext.getContext().getValueStack().set("allDepartment",allDepartment);
        return "preEdit";
    }
//-----------------------------------------------

    //员工的编辑更新
   public String edit(){
        //更新员工
       this.staffService.update(crmStaff);
       return "edit";
   }

    public StaffService getStaffService() {
        return staffService;
    }

    public void setStaffService(StaffService staffService) {
        this.staffService = staffService;
    }

    public DepService getDepService() {
        return depService;
    }

    public void setDepService(DepService depService) {
        this.depService = depService;
    }

    public List<CrmDepartment> getAllDepartment() {
        return allDepartment;
    }

    public void setAllDepartment(List<CrmDepartment> allDepartment) {
        this.allDepartment = allDepartment;
    }

    public CrmStaff getCrmStaff() {
        return crmStaff;
    }

    public void setCrmStaff(CrmStaff crmStaff) {
        this.crmStaff = crmStaff;
    }
    public String getStaffId() {
        return staffId;
    }
    public void setStaffId(String staffId) {
        this.staffId = staffId;
    }

}
